﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;

public partial class Admin_DonHangReport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["AdminName"] != null)
            {
                string trangthai = "Đặt hàng";
                string sql = string.Format(@"
                                    SELECT
                                        a.FullName,
                                        a.SDT,
                                        b.NgayDat,
                                        b.NgayNhan,
                                        c.MaXe,
                                        c.SoLuongMua,
                                        b.NoiNhan
                                    FROM Customers a INNER JOIN Orders b 
                                                 ON a.CustomerID = b.CustomerID
                                                 INNER JOIN OrderDetail c 
                                                 ON b.OrderID = c.OrderID
                                    WHERE b.TrangThai = N'{0}'
                                    ", trangthai);
                DataTable dt = DataBase.GetDataTable(sql);
                dsDonHang ds = new dsDonHang();
                int dong = -1;
                foreach (DataRow dtr in dt.Rows)
                {
                    dong++;
                    ds.dtDonHang.Rows.Add();
                    ds.dtDonHang.Rows[dong]["TenKH"] = dtr["FullName"].ToString();
                    ds.dtDonHang.Rows[dong]["SDT"] = dtr["SDT"].ToString();
                    ds.dtDonHang.Rows[dong]["NgayDat"] = dtr["NgayDat"].ToString();
                    ds.dtDonHang.Rows[dong]["NgayNhan"] = dtr["NgayNhan"].ToString();
                    ds.dtDonHang.Rows[dong]["MaXe"] = dtr["MaXe"].ToString();
                    ds.dtDonHang.Rows[dong]["SoLuong"] = dtr["SoLuongMua"].ToString();
                    ds.dtDonHang.Rows[dong]["NoiNhan"] = dtr["NoiNhan"].ToString();
                }
                ReportDocument objreport = new ReportDocument();
                objreport.Load(Server.MapPath("~/Admin/BaoCao/rpDonHang.rpt"));
                objreport.SetDataSource(ds);
                this.CrystalReportViewer1.ReportSource = objreport;
                CrystalReportViewer1.RefreshReport();
            }
            else
                Response.Redirect("Default.aspx");
        }        
    }
    protected void CrystalReportViewer1_Init(object sender, EventArgs e)
    {

    }
    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("BaoCao.aspx");
    }
}
